<?php
/*
 * 后台网校控制器
 */
class SchoolonlineController extends BasicController {
    
    private function init(){
        Yaf_Registry::get('tokenPlugin')->checkAuth(true);
    }

    /*
    * 网校新增
    */
    public function addAction(){
        $params = $this->getPost();
        
        $so_name = isset($params['name']) ? $params['name'] : '';
        $so_intro = isset($params['intro']) ? $params['intro'] : '';
        $so_contact = isset($params['contact']) ? $params['contact'] : '';
        $so_head_img = isset($params['head_img']) ? $params['head_img'] : '';
        $so_type = isset($params['type']) ? $params['type'] : '';
        $so_img = isset($params['img']) ? $params['img'] : '';
        $so_domain = isset($params['domain']) ? $params['domain'] : '';
        $am_id_1 = isset($params['am_id_1']) ? $params['am_id_1'] : 0;
        $am_id_2 = isset($params['am_id_2']) ? $params['am_id_2'] : 0;
        $am_id_3 = isset($params['am_id_3']) ? $params['am_id_3'] : 0;
        
        $return['code'] = '1';
        $return['msg'] = '操作失败！';
        
        if(!$so_name){
            echo json_encode($return);
            return false;
        }
        
        $data['so_name'] = $so_name;
        $data['so_intro'] = $so_intro;
        $data['so_contact'] = $so_contact;
        $data['so_head_img'] = $so_head_img;
        $data['so_type'] = $so_type;
        $data['so_img'] = $so_img;
        $data['so_domain'] = $so_domain;
        $data['am_id_1'] = $am_id_1;
        $data['am_id_2'] = $am_id_2;
        $data['am_id_3'] = $am_id_3;
        $data['create_dateline'] = CUR_TIMESTAMP;
        $data['modify_dateline'] = CUR_TIMESTAMP;
        
        $schoolOnlineModel = Helper::load('School_online');
        $r = $schoolOnlineModel->Insert($data);
        
        if($r){
            $return['code'] = '0';
            $return['msg'] = '操作成功！';
        }
        
        echo json_encode($return);
        return false;
    }
    
    /*
    * 网校编辑
    */
    public function editAction(){
        $params = $this->getPost();
        
        $so_id = isset($params['id']) ? $params['id'] : 0;
        $so_name = isset($params['name']) ? $params['name'] : '';
        $so_intro = isset($params['intro']) ? $params['intro'] : '';
        $so_contact = isset($params['contact']) ? $params['contact'] : '';
        $so_head_img = isset($params['head_img']) ? $params['head_img'] : '';
        $so_type = isset($params['type']) ? $params['type'] : '';
        $so_img = isset($params['img']) ? $params['img'] : '';
        $so_domain = isset($params['domain']) ? $params['domain'] : '';
        $am_id_1 = isset($params['am_id_1']) ? $params['am_id_1'] : 0;
        $am_id_2 = isset($params['am_id_2']) ? $params['am_id_2'] : 0;
        $am_id_3 = isset($params['am_id_3']) ? $params['am_id_3'] : 0;
        
        $return['code'] = '1';
        $return['msg'] = '操作失败！';
        
        if(!$so_id || !$so_name){
            echo json_encode($return);
            return false;
        }
        
        $data['so_name'] = $so_name;
        $data['so_intro'] = $so_intro;
        $data['so_contact'] = $so_contact;
        $data['so_head_img'] = $so_head_img;
        $data['so_type'] = $so_type;
        $data['so_img'] = $so_img;
        $data['so_domain'] = $so_domain;
        $data['am_id_1'] = $am_id_1;
        $data['am_id_2'] = $am_id_2;
        $data['am_id_3'] = $am_id_3;
        $data['modify_dateline'] = CUR_TIMESTAMP;
        
        $schoolOnlineModel = Helper::load('School_online');
        $r = $schoolOnlineModel->Where(['so_id' => $so_id])->UpdateOne($data);
        
        if($r){
            $return['code'] = '0';
            $return['msg'] = '操作成功！';
        }
        
        echo json_encode($return);
        return false;
    }
    
    /*
    * 网校详情
    */
    public function showAction(){
        $params = $this->getPost();
        
        $so_id = isset($params['id']) ? $params['id'] : 0;
        
        $return['code'] = '1';
        $return['msg'] = '获取失败！';
        $return['data'] = [];
        
        $schoolOnlineModel = Helper::load('School_online');
        
        $info = $schoolOnlineModel->Where(['so_id' => $so_id])->SelectOne();
        
        if(!empty($info) && is_array($info)){
            $return['code'] = '0';
            $return['msg'] = '操作成功！';
            
            $return['data'] = [
                    'id'        => $info['so_id'],//网校id
                    'name'      => $info['so_name'],//网校名称
                    'intro'     => $info['so_intro'],//简介
                    'contact'   => $info['so_contact'],//联系我们
                    'head_img'  => $info['so_head_img'],//网校顶部头图
                    'type'      => $info['so_type'],//网校类别，1：小学，2：初中，3：高中，4：一贯制
                    'img'       => $info['so_img'],//logo
                    'domain'    => $info['so_domain']//二级域名
            ];
        }
        
        echo json_encode($return);
        return false;
    }
    
    /*
    * 网校删除
    */
    public function delAction(){
        $params = $this->getPost();
        
        $so_id = isset($params['id']) ? $params['id'] : 0;
        
        $return['code'] = '1';
        $return['msg'] = '操作失败！';
        
        $schoolOnlineModel = Helper::load('School_online');
        
        $data['isdelete'] = 1;
        $data['modify_dateline'] = CUR_TIMESTAMP;
        
        $r = $schoolOnlineModel->Where(['so_id' => $so_id])->UpdateOne($data);
        
        if($r){
            $return['code'] = '0';
            $return['msg'] = '操作成功！';
        }
        
        echo json_encode($return);
        return false;
    }
    
    /*
    * 获取网校的列表
    */
    public function listAction(){
        $params = $this->getPost();
        
        $so_id = isset($params['id']) ? $params['id'] : '';
        $so_name = isset($params['name']) ? $params['name'] : '';
        $so_domain = isset($params['domain']) ? $params['domain'] : '';
        
        $return['code'] = '1';
        $return['msg'] = '获取失败！';
        $return['data'] = [];
        
        $schoolOnlineModel = Helper::load('School_online');
        
        $where = " isdelete = 0 ";
        $binds = [];
        
        if($so_id){
            $where.= " and so_id = ? ";
            $binds[] = $so_id;
        }
        
        if($so_name){
            $where.= " and so_name like '%".$so_name."%' ";
        }
        
        if($so_domain){
            $where.= " and so_domain = ? ";
            $binds[] = $so_domain;
        }
        
        $sql = "select * "
                . " from `".TB_PREFIX."school_online`"
                . " where {$where}"
                . " order by so_id desc";

        $res = $schoolOnlineModel->CompileBinds($sql, $binds)->Select();
        
        $data = [];
        
        if(!empty($res) && is_array($res)){
            foreach($res as $item){
                $data[] = [
                    'id'        => $item['so_id'],//网校id
                    'name'      => $item['so_name'],//网校名称
                    'intro'     => $item['so_intro'],//简介
                    'contact'   => $item['so_contact'],//联系我们
                    'head_img'  => $item['so_head_img'],//网校顶部头图
                    'type'      => $item['so_type'],//网校类别，1：小学，2：初中，3：高中，4：一贯制
                    'img'       => $item['so_img'],//logo
                    'domain'    => $item['so_domain']//二级域名
                ];
            }
        }
        
        $return['code'] = '0';
        $return['msg'] = '操作成功！';
        $return['data'] = $data;
        
        echo json_encode($return);
        return false;
    }
    
}